package com.xbongbong.workflow.service.strategy.node.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.workflow.domain.entity.WorkflowExecutionEntity;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.domain.entity.WorkflowTaskLogEntity;
import com.xbongbong.paas.config.enums.WorkflowExecutionStatusEnum;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.paas.config.enums.WorkflowTaskLogStatusEnum;
import com.xbongbong.paas.exception.WorkflowException;
import com.xbongbong.workflow.model.WorkflowExecutionModel;
import com.xbongbong.workflow.pojo.WorkflowProcessResultPojo;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.service.strategy.node.AbstractWorkflowNodeStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 开始节点
 * @author 魏荣杰
 *
 */
@Service
public class WorkflowNodeStartStrategy extends AbstractWorkflowNodeStrategy {

    @Resource
    private WorkflowExecutionModel workflowExecutionModel;


    @Override
    public String type() {
        return WorkflowNodeTypeEnum.START_NODE.getName();
    }

    @Override
    public void before(WorkflowTransferPOJO workflowTransferPOJO) {
        //NodeStart节点这里do nothing,日志的创建放在process里
    }

    @Override
    public WorkflowProcessResultPojo process(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity workflowNodeEntity, Long lastNodeId, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) {
        // 生成工作流执行器数据, 阶段推进器和工作流触发工作流需要生成执行器数据
        if (workflowTransferPOJO.getIsFromStage()) {

            Long parentExecutionId = workflowTransferPOJO.getParentExecutionId();

            WorkflowExecutionEntity workflowExecutionEntity = new WorkflowExecutionEntity();
            workflowExecutionEntity.setCorpid(workflowTransferPOJO.getCorpid());
            workflowExecutionEntity.setParentId(parentExecutionId);
            workflowExecutionEntity.setStatus(WorkflowExecutionStatusEnum.IN_PROGRESS.getCode());
            workflowExecutionEntity.setType(workflowTransferPOJO.getExecutionType());
            workflowExecutionEntity.setTaskId(workflowTransferPOJO.getTaskId() == null ? 0L : workflowTransferPOJO.getTaskId());
            workflowExecutionEntity.setLinkKey(workflowTransferPOJO.getLinkKey());

            long now = DateTimeUtil.getInt();
            workflowExecutionEntity.setAddTime(now);
            workflowExecutionEntity.setUpdateTime(now);
            workflowExecutionEntity.setDel(DelEnum.NORMAL.getDel());

            workflowExecutionModel.insert(workflowExecutionEntity);

            workflowTransferPOJO.setExecutionId(workflowExecutionEntity.getId());
        }

        // 生成日志
        logger(workflowTransferPOJO, workflowNodeEntity, WorkflowTaskLogStatusEnum.PASS, null);
        return new WorkflowProcessResultPojo(true, false);
    }

    @Override
    public void after(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity workflowNodeEntity) {
        super.after(workflowTransferPOJO, workflowNodeEntity);
    }

    @Override
    public void check() {

    }

    @Override
    public void logger(WorkflowTransferPOJO workflowTransferPOJO, WorkflowNodeEntity nodeEntity, WorkflowTaskLogStatusEnum workflowTaskLogStatusEnum, WorkflowException workflowException) {
        // 初始节点创建taskLog
        WorkflowTaskLogEntity workflowTaskLogEntity = new WorkflowTaskLogEntity();
        workflowTaskLogEntity.setCorpid(nodeEntity.getCorpid());
        workflowTaskLogEntity.setAppId(nodeEntity.getAppId());
        workflowTaskLogEntity.setFormId(nodeEntity.getFormId());
        workflowTaskLogEntity.setMenuId(nodeEntity.getMenuId());
        workflowTaskLogEntity.setWorkflowId(nodeEntity.getWorkflowId());
        workflowTaskLogEntity.setTaskId(workflowTransferPOJO.getTaskId() == null ? 0L : workflowTransferPOJO.getTaskId());
        workflowTaskLogEntity.setDel(DelEnum.NORMAL.getDel());
        workflowTaskLogEntity.setBusinessType(nodeEntity.getBusinessType());
        workflowTaskLogEntity.setSaasMark(nodeEntity.getSaasMark());

        //日志内容
        workflowTaskLogEntity.setContent(workflowException == null ? null : workflowException.getMsg());
        workflowTaskLogEntity.setStatus(workflowTaskLogStatusEnum.getType());

        long now = DateTimeUtil.getInt();
        workflowTaskLogEntity.setExitTime(0L);
        workflowTaskLogEntity.setUpdateTime(now);
        workflowTaskLogEntity.setAddTime(now);
        workflowTaskLogEntity.setLinkKey(workflowTransferPOJO.getLinkKey());

        workflowTaskLogModel.insert(workflowTaskLogEntity);
        Long logId = workflowTaskLogEntity.getId();
        // 设置工作流日志id
        workflowTransferPOJO.setLogId(logId);
        // 创建log的详情
        createTaskLogNodeItem(workflowTransferPOJO, nodeEntity, WorkflowTaskLogStatusEnum.PASS, null, logId);
    }


}
